package com.hliushi.demo.service.impl;


import com.hliushi.demo.entity.CityDictionary;
import com.hliushi.demo.repository.CityDictionaryRepository;
import com.hliushi.demo.service.CityDictionaryService;
import com.iflytek.bp.common.repository.Criteria;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author llhuang10
 * @date 2021/7/21 12:55
 */
@Slf4j
@Service
public class CityDictionaryServiceImpl implements CityDictionaryService {

    @Autowired
    private CityDictionaryRepository cityDictionaryRepository;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public CityDictionary addDict(CityDictionary cityDictionary) {
        return cityDictionaryRepository.save(cityDictionary);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public CityDictionary update(CityDictionary dictionaryService) {
        return cityDictionaryRepository.save(dictionaryService);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(String dictId) {
        cityDictionaryRepository.deleteById(Long.parseLong(dictId));
    }

    @Override
    @Transactional(readOnly = true)
    public List<CityDictionary> getList() {
        return cityDictionaryRepository.findAll();
    }


    @Override
    @Transactional(readOnly = true)
    public Page<CityDictionary> getListPage(Pageable pageable) {
        // Specification<CityDictionary> queryCondition = (Specification<CityDictionary>) (root, query, criteriaBuilder) -> null;
        //
        // return cityDictionaryRepository.
        //         findAll(queryCondition,
        //                 PageRequest.of(
        //                         pageable.getPageNumber(),
        //                         pageable.getPageSize(),
        //                         Sort.by(Sort.Direction.ASC, "id")
        //                 ));

        return cityDictionaryRepository.search(
                Criteria.create().greaterThan(CityDictionary.Fields.provCode, 1),
                pageable);
    }
}
